#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>

using namespace std;

#define Format "[%s@%s: ~%s]# "
#define PromptSize 1024
#define MAXSIZE 256

const char* get_name()
{
	const char* name = getenv("USER");
	if(name == NULL)
		return NULL;
	
	return name;
}

const char* get_hostname(){
	const char* hostname = getenv("HOSTNAME"); 
	if(hostname == NULL)
		return NULL;
	
	return hostname;
}

const char* get_pwd(){
	const char* pwd = getenv("PWD");
	if(pwd == NULL)
		return NULL;
	return pwd;
}


//  1、打印环境变量信息
void EV_Infor_Print(char Attribute[], int size){
	snprintf(Attribute, size, Format, get_name(), get_hostname(), get_pwd());
}

void PrintPrompt()
{
	char prompt[PromptSize];
	EV_Infor_Print(prompt, sizeof(prompt));
	printf("%s", prompt);
	fflush(stdout);
}

//2、输入命令信息
bool GetCommand(char *out, int size)
{
	fgets(out, size, stdin);
	out[strlen(out)-1] = 0;
	if(strlen(out) == 0)
		return false;
	
	return true;
}

int main()
{
	//1、打印属性信息
	PrintPrompt();

	//2、输入命令信息
	char Commandline[MAXSIZE];
	if(GetCommand(Commandline, sizeof(Commandline)))
		printf("echo %s\n", Commandline);
	return 0;
}
